<?php
namespace app\common\library\xilujob\resume;


use app\common\library\Auth;
use app\common\library\xilujob\ResumeLibrary;
use app\common\model\xilujob\ResumePersonalTag;
use app\common\model\xilujob\Resume;
use app\common\model\xilujob\ResumePersonalTagRelation;
use think\Db;
use function fast\array_get;

class PersonalTag extends BaseModule {

    protected $name = ResumeLibrary::MODULE_PERSONAL_TAG;

    public function onCreateResume(Resume $resume) {}

    public function add($data) {
        $tags = array_get($data, 'tags', []);
        if(!is_array($tags)) {
            $tags = explode(',', $tags);
        }
        try {
            Db::startTrans();
            $tagsIds = [];
            foreach ($tags as $tagName) {
                $tagId = ResumePersonalTag::where('name',$tagName)->value('id');
                if($tagId) {
                    $tagsIds[] = $tagId;
                }else if(mb_strlen($tagName,'utf-8')>8) {
                    continue;
                }else {
                    $tagId = ResumePersonalTag::create(['name'=>$tagName, 'user_id'=>Auth::instance()->id?:0])->id;
                    $tagsIds[] = $tagId;
                }
            }
            if(!$tagsIds) {
                ResumePersonalTagRelation::where('resume_id', $this->resume->id)->delete();
            }else {
                $existedTagIds = ResumePersonalTagRelation::where('resume_id', $this->resume->id)
                    ->where('tag_id','in',$tagsIds)
                    ->column('tag_id');
                $insertIds = array_diff($tagsIds, $existedTagIds);
                foreach ($insertIds as $tagId) {
                    ResumePersonalTagRelation::create([
                        'user_id'=>$this->resume->user_id,
                        'resume_id'=>$this->resume->id,
                        'tag_id'=>$tagId
                    ]);
                }
                ResumePersonalTagRelation::where('resume_id', $this->resume->id)
                    ->where('tag_id', 'not in', $tagsIds)
                    ->delete();
            }
            Db::commit();
            return ResumePersonalTag::where('id', 'in', $tagsIds)->select();
        }catch (\Exception $e) {
            Db::rollback();
            throw $e;
        }
    }

    public function edit($data) {
        throw new ModuleException("Module '{$this->name}' does not support edit method");
    }

    public function del($id) {
        throw new ModuleException("Module '{$this->name}' does not support del method");
    }

    /**
     * 获取所有字段的key
     */
    protected function keys() {
        return ['avatar','realname','gender','birthdate','education_level_id','work_experience_id','hukou_city','current_state_id','idcard_no','email','mobile'];
    }

    /**
     * 获取验证规则
     * @return array
     */
    protected function getRules() {
        return [];
    }

    /**
     * 获得Model实例
     */
    protected function getModel() {
        return new ResumePersonalTagRelation();
    }

    /**
     * @return array score: 当前分数， max: 满分
     */
    public function getCompleteScore() {
        $count = $this->getModel()->where('resume_id', $this->resume->id)->count();
        return ['score'=>$count?10:0, 'max'=>10];
    }

}